package com.weight.netty;

import org.java_websocket.WebSocket;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Scanner;
/**
 * 客户端用于测试
 * @author jmk
 *
 */
public class WebsocketClient {

    public static WebSocketClient client;
    public static void main(String[] args) {
        try {
            client = new WebSocketClient(new URI("//localhost:8083"),new Draft_6455()) {
                @Override
                public void onOpen(ServerHandshake serverHandshake) {
                    System.out.println(("握手成功"));
                }

                @Override
                public void onMessage(String msg) {
                    System.out.println(("客户端收到消息："+msg));
                }

                @Override
                public void onClose(int i, String s, boolean b) {
                    System.out.println("链接已关闭");
                }

                @Override
                public void onError(Exception e){
                    e.printStackTrace();
                    System.out.println(("发生错误已关闭"));
                }
            };
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

        client.connect();
        while(!client.getReadyState().equals(WebSocket.READYSTATE.OPEN)){
            System.out.println(("正在连接..."));
        }
        while (true){
            Scanner sc = new Scanner(System.in);
            String x = sc.nextLine();
            //连接成功,发送信息
            client.send(x);
        }
    }
} 
